package com.tokenplus.thai.base

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.View
import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.lifecycle.ViewModel
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout.OnRefreshListener
import com.tokenplus.thai.R
import com.tokenplus.thai.components.dialog.LoadingDialog
import com.tokenplus.thai.anetwork.NetworkException

abstract class BaseFragment : Fragment() {

    private lateinit var mLoadingDialog: LoadingDialog

    private var mSwipeRefresh: SwipeRefreshLayout? = null

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        activity?.let {
            mLoadingDialog = LoadingDialog(it)
        }
        setViewModelActions()
    }

    /**绑定model*/
    protected abstract fun onBindingViewModel(): List<ViewModel>?

    private fun setViewModelActions() {
        val viewModelList = onBindingViewModel()
        if (!(viewModelList.isNullOrEmpty())) {
            viewModelList.forEach {
                if (it is BaseViewModelAction) {
                    it.getLoadingLiveData()
                        .observe(viewLifecycleOwner) { showLoading ->
                            if (showLoading)
                                showLodingDialog()
                            else
                                dismissLoadingDialog()
                        }

                    it.getNetworkExceptionLiveData()
                        .observe(viewLifecycleOwner) {
                            onHandleNetworkExcepton(it)
                        }
                }
            }
        }
    }

    protected fun showLodingDialog() {
        activity?.let {
            mLoadingDialog.showDialog(it)
        }
    }

    protected fun dismissLoadingDialog() {
        mLoadingDialog.dismissDialog()
    }

    /**交给子类去实现*/
    open fun onHandleNetworkExcepton(networkException: NetworkException) {
        activity?.let {
            if (networkException.isAlter()) {
//                MessageDialog.showDialog(it.supportFragmentManager, networkException.msg)
            } else {
                Toast.makeText(it, networkException.msg, Toast.LENGTH_LONG)
                    .show()
            }
        }
    }

    /**交给子类去实现*/
    open fun requestSpecPermissions(permissionList: Array<String>) {
        try {
            (requireContext() as BaseActivity).requestSpecPermissions(
                permissionList
            )
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /** 关联刷新 */
    open fun connectSwipeRefreshLayout(layout: SwipeRefreshLayout?, listener: OnRefreshListener) {
        mSwipeRefresh = layout

        activity?.let { curActivity ->
            mSwipeRefresh?.apply {
                setColorSchemeColors(
                    ContextCompat.getColor(
                        curActivity,
                        R.color.main_blue
                    )
                )

                setOnRefreshListener(listener)
            }
        }
    }

    /** 关闭刷新 */
    open fun stopRefreshState() {
        mSwipeRefresh?.isRefreshing = false
    }

    /** 打开刷新 */
    open fun startRefreshState() {
        mSwipeRefresh?.isRefreshing = true
    }
}